class Report < ActiveRecord::Base
  
  has_many :comments,:foreign_key=>"obj_id", :conditions=>"category_id=2"
  has_many :indices, :foreign_key=>"obj_id", :conditions=>"category_id=2"
  has_many :report_educations,:class_name=>"ReportStatistic::ReportEducation", :foreign_key => "report_id"
  has_many :report_overseas, :class_name=>"ReportStatistic::ReportOversea",:foreign_key => "report_id"
  has_many :report_exps, :class_name=>"ReportStatistic::ReportExp",:foreign_key => "report_id" 
  has_many :report_ages, :class_name=>"ReportStatistic::ReportAge",:foreign_key => "report_id" 
  has_many :report_sexes, :class_name=>"ReportStatistic::ReportSex",:foreign_key => "report_id" 
  has_many :report_salaries, :class_name=>"ReportStatistic::ReportSalary",:foreign_key => "report_id" 
  belongs_to :user,:foreign_key=>"user_id", :counter_cache => true, :conditions=>"id!=0"
  
  def serialize_report_education
    array='['
    data1='['
    data2='['
    self.report_educations.each do |data|
      array += '"' + data.education.name + '",'
      data1 += data.lower.to_s + ','
      data2 += data.value.to_s + ','
    end
    array=array[0..-2] + ']'
    data1=data1[0..-2] + ']'
    data2=data2[0..-2] + ']'
    return array,data1,data2
  end
  
  def serialize_report_oversea
    array='['
    data1='['
    data2='['
    self.report_overseas.each do |data|
      array += '"' + data.oversea.name + '",'
      data1 += data.lower.to_s + ','
      data2 += data.value.to_s + ','
    end
    array=array[0..-2] + ']'
    data1=data1[0..-2] + ']'
    data2=data2[0..-2] + ']'
    return array,data1,data2
  end
  
  def serialize_report_exp
    return Report.serialize_report_statistic(self.report_exps)
  end
  
  def serialize_report_age
    return Report.serialize_report_statistic(self.report_ages)
  end
  
  def serialize_report_sex
    return Report.serialize_report_statistic(self.report_sexes)
  end
  
  def serialize_report_salarie
    salary_range='['
    incumbent_count='['
    self.report_salaries.each do |data|
      salary_range += '"' + data.lower + '~'+ data.upper + '",'
      incumbent_count += data.value.to_i + ','
    end
    salary_range=salary_range[0..-2] + ']'
    incumbent_count=incumbent_count[0..-2] + ']'
    return array,data1,data2
  end
  
  def copy_from_incumbent(incumbent)
    self.virtual=0
    self.state=3
    self.div_salary=0   
    self.aver_salary=incumbent.salary
    self.aver_age=incumbent.report_age
    self.aver_exp=incumbent.report_experience
    self.aver_rank=incumbent.report_sbrank
    self.salary_25til=incumbent.salary
    self.salary_50til=incumbent.salary
    self.salary_75til=incumbent.salary
    self.report_educations=[]
    self.report_overseas=[]
    self.report_salaries=[]
    self.report_educations<<ReportStatistic::ReportEducation.create_from_incumbent(incumbent)
    self.report_overseas<<ReportStatistic::ReportOversea.create_from_incumbent(incumbent)
    self.report_salaries<<ReportStatistic::ReportSalary.create_from_incumbent(incumbent)
    self.report_exps<<ReportStatistic::ReportExp.create_from_incumbent(incumbent)
    self.report_ages<<ReportStatistic::ReportAge.create_from_incumbent(incumbent)
    self.report_sexes<<ReportStatistic::ReportSex.create_from_incumbent(incumbent)
    self.incumbents_count = 1
  end
  
  def new?
    return self.incumbents_count==1 ? true : false
  end
  
  def before_create
    self.created_at=Time.now
    self.updated_at=Time.now
    self.create_search_tag
  end
  
  def after_create
    Index.index_search_tag(self)
  end
  
  def before_destroy  
    Comment.delete_all(["category=2 and obj_id = ?", self.id])
    Indexer.delete_all(["category=2 and obj_id = ?", self.id])
    ReportEducation.delete_all(["report_id = ?", self.id])
    ReportOversea.delete_all(["report_id = ?", self.id])
    Statistic.destroy_all(["category=2 and obj_id=?",self.id])
    Hot_list.destroy_all(["category=2 and obj_id=?",self.id])
  end
  
  def select_incumbent(incumbent)
     incumbents.push_with_attributes(incumbent, :included => 0)
  end
  
  def include_incumbent(inc,included)
    if included
     incumbents.delete(inc)
     incumbents.push_with_attributes(inc, :included => 1)
    else
     incumbents.delete(inc)
     incumbents.push_with_attributes(inc, :included => 0)
    end
  end
  
  def self.validate_params(params)
    validation=[]
    validation<<["<li>请输入有效的报告名称</li>"] unless params[:title]=~/[\d\w]+/&&params[:title].length<50
    validation<<["<li>请选择有效的报告年份</li>"] unless params[:salary]==params[:password_confirmation]
    return validation
  end
  
  def self.serialize_report_statistic(statistics)
    array='['
    data1='['
    data2='['
    statistics.each do |data|
      array += '"' + StatisticCategory.find(data.upper).name + '",'
      data1 += data.lower.to_s + ','
      data2 += data.value.to_s + ','
    end
    array=array[0..-2] + ']'
    data1=data1[0..-2] + ']'
    data2=data2[0..-2] + ']'
    return array,data1,data2
  end
end

class CompanyReport<Report
  has_one :company,:foreign_key=> "company_report_id"
  
  def create_search_tag
   self.search_tag=self.company.search_tag
  end
  
  def self.create_from_incumbent(incumbent)
    report=CompanyReport.new
    report.copy_from_incumbent(incumbent)
    report.city_id = 0
    report.position_id=0
    report.effective_year=0
    report.company=incumbent.report_company
    report.name=report.company.name
    return report   
  end
  
  def self.get_report(incumbent)
    incumbent.report_company.report||Report::CompanyReport.create_from_incumbent(incumbent)
  end
  
end

class CityReport<Report
  belongs_to :city,:class_name => "City",:foreign_key => "city_id"
    
  def create_search_tag
    self.search_tag=self.name + " "
    self.search_tag << self.city.region.name   
  end
    
  def self.create_from_incumbent(incumbent)
    report=CityReport.new
    report.copy_from_incumbent(incumbent)
    report.city=incumbent.report_city
    report.name=incumbent.effective_year.to_s + " " + report.city.name
    report.position_id=0
    report.effective_year=incumbent.effective_year
    return report    
  end
    
  def self.get_report(incumbent)
    Report::CityReport.find(:first, 
                            :conditions => ["effective_year=? and city_id = ?",incumbent.effective_year,incumbent.city_id])||Report::CityReport.create_from_incumbent(incumbent)
  end
      
end

class PositionReport<Report
  belongs_to :position,:class_name => "Position",:foreign_key => "position_id"
  belongs_to :city,:class_name => "City",:foreign_key => "city_id"
  
  def create_search_tag
    self.search_tag=self.name + " "
    self.search_tag<< self.effective_year.to_s + " "
    self.search_tag << self.city.name + " "
    self.search_tag << self.city.region.name + " "
    self.search_tag << self.position.function.name
  end
  
  def self.create_from_incumbent(incumbent)
    report=PositionReport.new
    report.copy_from_incumbent(incumbent)
    report.city=incumbent.report_city
    report.position=incumbent.report_position
    report.effective_year=incumbent.effective_year
    report.name=report.position.name
    return report    
  end
  
  def self.get_report(incumbent)
    Report::PositionReport.find(:first, 
                                :conditions => ["effective_year=? and city_id = ? and position_id=?",incumbent.effective_year,incumbent.city_id,incumbent.position_id])||Report::PositionReport.create_from_incumbent(incumbent)
  end
  
end

class CustomizedReport<Report
  has_and_belongs_to_many :incumbents,:class_name=>"Incumbent"
  
  def create_search_tag
    self.search_tag<<" "+self.name
  end
  
  def copy_from_position_report(report)
    self.virtual=report.id
    self.state=report.state                      
    self.div_salary=report.div_salary
    self.updated_at=report.updated_at
    self.incumbent_count=report.incumbent_count
    self.aver_salary=report.aver_salary
    self.aver_age=report.aver_age
    self.aver_exp=report.aver_exp
    self.aver_rank=report.aver_rank
    self.salary_75til=report.salary_75til
    self.salary_25til=report.salary_25til
    self.salary_50til=report.salary_50til
    self.effective_year=report.effective_year             
    self.search_tag=report.search_tag
  end
  
  
  def self.create_from_incumbent(incumbent)
    report=CustomizedReport.new
    report.city_id=0
    report.position_id=0
    report.name=incumbent.name
    report.user=incumbent.user
    report.reference_salary=incumbent.salary
    return report 
  end
  
end
